import { _decorator, Component, instantiate, Label, Node, Prefab } from 'cc';
import { UIBase } from './UIBase';
import { ModuleBase } from './Card/ModuleBase';
import { ModuleMgr } from './Card/ModuleMgr';
import { Cards } from './Card/Cards';
import { GameData } from './GameData';
import { Score } from './Function/Score';
import { PlayNumAndDisNum } from './Function/PlayNumAndDisNum';
import { UIMgr } from './UIMgr';
import { Settlement } from './Settlement';
import { CardMgr } from './Card/CardMgr';
import { Cardnum } from './Function/Cardnum';
import { Goldnum } from './Function/Goldnum';
import { resmgr } from './Card/ResMgr';
import { PrefabMgr } from './Card/SpecialResMgr';
import { JokerMgr } from './Function/JokerMgr';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends UIBase {
    isover:Boolean=false;
    private _isRestart:boolean=true;
    fristStart:boolean=true;
    set isRestart(isRestart:boolean){
        this._isRestart=isRestart;
    }
    private jokerMgr=null;
    oninit(...rest: any[]): void {
        //将JokerMgr添加
        let prefab=resmgr.getMgr<PrefabMgr>("PrefabMgr").getRes<Prefab>("JokerMgr");
        let jokerMgr=instantiate(prefab);
        jokerMgr.parent=this.node.parent;
        jokerMgr.setPosition(0,290);
        jokerMgr.setSiblingIndex(100);
        let ts=jokerMgr.getComponent(JokerMgr);
        ModuleMgr.instance.addModuel(jokerMgr.name,ts);
        this.jokerMgr=ts;

        this._registerModuel();
        ModuleMgr.instance.OnInitModuel();
        //console.log(ModuleMgr.instance._mapModule)
        //注册弃牌出牌整理牌按钮
        this.addClickEventL("_DisCardBtn", () => {
            let cards = this.node.getChildByName("Cards").getComponent(Cards);
            cards.foldCard();
        })
        this.addClickEventL("_PlayCardBtn", () => {
            let cards = this.node.getChildByName("Cards").getComponent(Cards);
            cards.playCard();
        })
        this.addClickEventL("_SortBtn", () => {
            let cards=this.node.getChildByName("Cards").getComponent(Cards);
            cards.sortCard();
        })
        //按钮隐藏
        this.getNode("_DisCardBtn").active=false;        
        this.getNode("_PlayCardBtn").active=false;
        //设置目标分数
        this.getComp<Label>("_NeedScore","Label").string=GameData.levelscore.toString();
    }
    reStart(){
        //将jokerMgr激活
        this.jokerMgr.node.active=true;
        this.jokerMgr.onInit();
        //设置目标分数
        this.getComp<Label>("_NeedScore","Label").string=GameData.levelscore.toString();
        //牌数还原
        ModuleMgr.instance.getModuel<Cardnum>("Cardnum").onInit();
        //重整牌堆
        this.node.getChildByName("CardMgr").getComponent(CardMgr).reStart();
        //清空分数
        this.getComp<Label>("_ScoreLabel","Label").string="0";
        this.node.getChildByName("Score").getComponent(Score)._score=0;
        //抽牌弃牌数还原
        ModuleMgr.instance.getModuel<PlayNumAndDisNum>("PlayNumAndDisNum").onInit();
        //金币数还原
        ModuleMgr.instance.getModuel<Goldnum>("Goldnum").onInit();
    }
    private _registerModuel(){
        let children=this.node.children;
        for(let child of children){
            let module=child.getComponent(ModuleBase);
            if(!module){
                continue;
            }
            ModuleMgr.instance.addModuel(child.name,module);
        }
        //console.log(ModuleMgr.instance.mapModule);

    }
    protected update(dt: number): void {
        ModuleMgr.instance.OnUpdateModuel(dt);
        //插入isRestart因为重启时可能会直接跳转到赢
        if(!this._isRestart||this.isover){
            return;
        }
        if(this.getNode("_ScoreLabel").parent.getComponent(Score)._score>=GameData.levelscore){
            this.isover=true;
            this.scheduleOnce(()=>{this.GameWin()},1);
            return;//防止刚好出牌数为0进入失败
        }
        if (this.getNode("_Playnum").parent.getComponent(PlayNumAndDisNum)._playnum==0) {
            this.isover=true;
            this.scheduleOnce(() => { this.GameOver() }, 1);
        }
    }
    //游戏失败结束调用函数
    GameOver(){
        this.closeUI("Game",true);
        this.openUI("Lose")
    }
    //游戏赢调用函数
    GameWin(){
        this._isRestart=false;
        //传入剩余出牌次数
        this.closeUI("Game");
        this.openUI("Settlement");
        let playCardnum=this.getNode("_Playnum").parent.getComponent(PlayNumAndDisNum)._playnum;
        GameData.playCardnum=playCardnum;
    }

}


